import VueRouter from "vue-router";
import store from '@/store'
// import Home from '../pages/Home/Home.vue'
// import Hobby from '../pages/Hobby.vue'
// import Manage from '../pages/Manage.vue'
// import About from '../pages/About.vue'
// import Login from '../pages/Login/Login.vue'
// import Classify from '../pages/Classify.vue'
// import Sort1 from '../pages/Sort1.vue'
// import Sort2 from '../pages/Sort2.vue'
// import Sort3 from '../pages/Sort3.vue'
// import Sort4 from '../pages/Sort4.vue'
// import Sort5 from '../pages/Sort5.vue'
// import Sort6 from '../pages/Sort6.vue'
// import Sort7 from '../pages/Sort7.vue'
// import Sort8 from '../pages/Sort8.vue'
// import { resolve } from "core-js/fn/promise";

const router =  new VueRouter({
    routes:[
        {
            path:'/home',
            component:() => import('../pages/Home/Home.vue'),
            meta:{title:'首页',show:true},
        },
        {
            path:'/hobby',
            component:()=>import('../pages/Hobby.vue'),
            meta:{title:'爱好|书籍',show:true}
        },
        {
            path:'/manage',
            component:()=>import('../pages/Manage.vue'),
            meta:{title:'管理内容',show:true}
        },
        {
            path:'/about',
            component:()=>import('../pages/About.vue'),
            meta:{title:'关于',show:true}
        },
        {
            path:'/publish',
            component:()=>import('../pages/Publish.vue'),
            meta:{title:'发布文章',show:true}
        },
        {
            path:'/login',
            component:()=>import('../pages/Login/Login.vue'),
            meta:{title:'登录',show:false},
        },
        {
            path:'/classify',
            component:()=>import('../pages/Classify.vue'),
            meta:{title:'分类',show:false},
            children:[
                {
                    path:'c',
                    component:()=>import('../pages/Sort/Sort1.vue'),
                    meta:{title:'C语言',show:false},
                    children:[
                        {
                            path:'cdetail1',
                            component:()=>import('../pages/Detail/Cdetail1'),
                            meta:{title:'详情',show:true}
                        }
                    ]
                },
                {
                    path:'qianduan',
                    component:()=>import('../pages/Sort/Sort2.vue'),
                    meta:{title:'前端学习',show:false},
                },
                {
                    path:'xuexi',
                    component:()=>import('../pages/Sort/Sort3.vue'),
                    meta:{title:'学习计划',show:false},
                },
                {
                    path:'shiyong',
                    component:()=>import('../pages/Sort/Sort4.vue'),
                    meta:{title:'实用技术',show:false},
                },
                {
                    path:'kaifa',
                    component:()=>import('../pages/Sort/Sort5.vue'),
                    meta:{title:'开发基础',show:false},
                },
                {
                    path:'wei',
                    component:()=>import('../pages/Sort/Sort6.vue'),
                    meta:{title:'微服务核心',show:false},
                },
                {
                    path:'lishi',
                    component:()=>import('../pages/Sort/Sort7.vue'),
                    meta:{title:'日本历史',show:false},
                },
                {
                    path:'riben',
                    component:()=>import('../pages/Sort/Sort8.vue'),
                    meta:{title:'日本流行音乐史',show:false},
                },
        ], 
    },
    {
        path:'*',
        redirect:"/home"
    },
    ]
})


router.beforeEach(async (to,from,next) => {
    let hasToken = store.state.user.token;
    if(hasToken){
        if(to.path == "/login"){
            next('/home');
        }else{
            next();
            // this.$store.dispatch('user/getInfo').then((message) => {
            //     console.log(message)
            //     NProgress.done()
            //     next()
            //   }).catch((message) => {
            //     next(`/login?redirect=${to.path}`)
            //     console.log(message)
            //     Message.error(message)
            //     NProgress.done()
            //   })
        }
    }else{
        // next(`/login?redirect=${to.path}`)
        next();
    }
})


//全局后置路由守卫——初始化的时候被调用、切换路由后被调用
router.afterEach((to,from)=>{
    document.title = to.meta.title || '首页'
    window.scrollTo(0,0)
})


export default router